package com.action.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bean.ResponseBean;
import com.pojo.Area;
import com.pojo.Country;
import com.pojo.Zhuji;
import com.service.AreaService;
import com.service.CountryService;
import com.service.ZhujiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/app_guojia")
public class AppCountryAction {

    @Autowired
    private CountryService countryService;
    @Autowired
    private AreaService areaService;
    @Autowired
    private ZhujiService zhujiService;
    /**
     * 查询所有的国家
     * @return
     */
    @RequestMapping("/selectCountrys")
    public ResponseBean selectCountrys(){
        List<Country> list = countryService.list();
        return new ResponseBean(0,list.size(),"查询成功",list);
    }

    /**
     * 查询所有的地区
     * @return
     */
    @RequestMapping("/selectAreas")
    public ResponseBean selectAreas(){
        QueryWrapper<Area> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("country_id");
        List<Area> list = areaService.list(queryWrapper);
        return new ResponseBean(0,list.size(),"查询成功",list);
    }
    /**
     * 根据国家ID查区域
     * @param country_id  国家ID
     * @return
     */
    @RequestMapping("/selectAreasBycountryID")
    public ResponseBean selectAreasBycountryID(String country_id){
        QueryWrapper<Area> queryWrapper = new QueryWrapper();
        queryWrapper.eq("country_id",country_id);
        List<Area> list = areaService.list(queryWrapper);
        List<Area> areas = new ArrayList<>();
        list.stream().forEach(a->{
            QueryWrapper<Zhuji> query = new QueryWrapper();
            query.eq("status",1);
            query.eq("area_id",a.getId());
            query.apply("lianjie_num > sy_lianjie_num");
            List<Zhuji> zjs = zhujiService.list(query);
            if(zjs.size() > 0){
                zjs.stream().forEach(z->{
                    if(a.getId().equals(z.getArea_id())){
                        areas.add(a);
                    }
                });
            }
        });
        return new ResponseBean(0,areas.size(),"查询成功",areas);
    }
}
